package com.yanqu.road.server.cmd.strongholdwar;

import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.activity.stronghold.CrossStrongholdUserRoungDamage;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.server.cmd.crossunion.CrossUnionMemberRankValueCmd;
import com.yanqu.road.server.manager.union.CrossUnionActivityDataMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.pb.activity.StrongholdActivityProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdActivityMgr;
import com.yanqu.road.server.manager.strongholdwar.CrossStrongholdMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Cmd(code = Protocol.C_CROSS_STRONGHOLD_MEMBER_CONDITION_DATA, desc = "跨服查询跨服据点战成员贡献信息")
public class CrossStrongholdMemberConditionDataCmd extends CrossUnionCommand {


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {

        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        StrongholdActivityProto.StrongholdWarActivityConditionUnionMemberDataReqMsg reqMsg = StrongholdActivityProto.StrongholdWarActivityConditionUnionMemberDataReqMsg.parseFrom(packet.getBytes());
        int type = reqMsg.getType();
        long reqUserId = packet.getPlayerId();
        int activityId = reqMsg.getActivityId();
        String unionId = reqMsg.getUnionUid();
        if (type == eGamePlayerEventType.StrongholdWildUnionScoreRank.getValue()) {
            StrongholdActivityProto.StrongholdActivityConditionUnionMemberDataRespMsg.Builder respMsg = StrongholdActivityProto.StrongholdActivityConditionUnionMemberDataRespMsg.newBuilder();
            respMsg.setRet(ret);
            UnionActivityConditionData unionActivityConditionData = CrossUnionActivityDataMgr.getUnionActivityConditionData(activityId, type, unionId);
            if (null != unionActivityConditionData) {
                ActivityProto.ActivityConditionUnionMemberDataListMsg.Builder dataMsg = CrossUnionMemberRankValueCmd.parseActivityConditionUnionMemberDataListMsg(
                        CrossUnionActivityDataMgr.getUserActivityConditionDataList(activityId, type, unionActivityConditionData.getMemberUserIdList()));
                respMsg.setDataList(dataMsg);
            }else {
                ret = GameErrorCode.E_ACTIVITY_NO_JOIN;
            }
            respMsg.setRet(ret);
            YanQuMessage message = YanQuMessageUtils.buildMessage(Protocol.U_STRONGHOLD_ACTIVITY_MEMBER_CONDITION_DATA, respMsg);
            MessageHelper.sendPacket(serverId, reqUserId, message);
        } else {
            Map<Integer, List<CrossStrongholdUserRoungDamage>> damageMap = CrossStrongholdMgr.getUserDamageMap(activityId, unionId);
            Collection<Integer> roundList = CrossStrongholdActivityMgr.getEliminateRoundList(activityId);
            int param = 1;
            Map<Long, BigInteger> userDamageMap = new HashMap<>();
            Map<Long, UserBaseInfo> unionBaseInfoMap = new HashMap<>();
            for (Integer round : roundList) {
                List<CrossStrongholdUserRoungDamage> damageList = damageMap.get(round);
                if (damageList != null) {
                    for (CrossStrongholdUserRoungDamage damage : damageList) {
                        long userId = damage.getUserId();
                        BigInteger value = damage.getDamage();
                        if (!userDamageMap.containsKey(userId)) {
                            userDamageMap.put(userId, BigInteger.ZERO);
                        }
                        BigInteger curValue = userDamageMap.get(userId).add(value.multiply(BigInteger.valueOf(param)));
                        userDamageMap.put(userId, curValue);
                        unionBaseInfoMap.put(userId, damage.getUserBaseInfo());
                    }
                }
                param = param * 2;
            }
            BigInteger totalValue = BigInteger.ZERO;

            for (BigInteger value : userDamageMap.values()) {
                totalValue = totalValue.add(value);
            }
            BigDecimal totalDamage = new BigDecimal(totalValue);
            BigDecimal unionScore = CrossStrongholdMgr.getUnionScore(activityId, unionId);
            StrongholdActivityProto.StrongholdActivityConditionUnionMemberDataRespMsg.Builder respMsg = StrongholdActivityProto.StrongholdActivityConditionUnionMemberDataRespMsg.newBuilder();
            respMsg.setRet(ret);
            ActivityProto.ActivityConditionUnionMemberDataListMsg.Builder dataListMsg = ActivityProto.ActivityConditionUnionMemberDataListMsg.newBuilder();
            for (Map.Entry<Long, BigInteger> entry : userDamageMap.entrySet()) {
                Long userId = entry.getKey();
                BigInteger value = entry.getValue();
                BigDecimal useScore = new BigDecimal(value).multiply(unionScore).divide(totalDamage, 10, BigDecimal.ROUND_HALF_EVEN);
                ActivityProto.ActivityConditionUnionMemberDataTempMsg.Builder tempMsg = ActivityProto.ActivityConditionUnionMemberDataTempMsg.newBuilder();
                tempMsg.setUserId(userId);
                tempMsg.setModifyTime(0);
                tempMsg.setNickName(unionBaseInfoMap.get(userId).getNickName());
                tempMsg.setValue(useScore.toBigInteger().toString());
                dataListMsg.addDataList(tempMsg);
            }
            respMsg.setDataList(dataListMsg);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_STRONGHOLD_ACTIVITY_MEMBER_CONDITION_DATA, respMsg);
            MessageHelper.sendPacket(serverId, reqUserId, pbMsg);
        }
    }

    @Override
    public void sendData() {

    }

}
